/*
 * Created by JFormDesigner on Mon Jul 01 16:28:51 GMT+08:00 2024
 */

package com.hongyan.view.admin;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.*;

import com.hongyan.base.BaseContentJpanel;
import com.hongyan.bean.Admin;
import com.hongyan.bean.Student;
import com.hongyan.config.MainFrameConfig;
import com.hongyan.dao.UserDao;
import net.miginfocom.swing.*;

/**
 * @author admin
 */
public class SwingAdminInfoPanel extends BaseContentJpanel {
    private static String[] sex = {"男", "女"};

    public SwingAdminInfoPanel() {
        super();
        initData();
    }

    @Override
    public void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        label3 = new JLabel();
        label4 = new JLabel();
        textField3 = new JTextField();
        comboBox1 = new JComboBox(sex);
        button1 = new JButton();

        //======== this ========
        setLayout(null);

        //---- label1 ----
        label1.setText("\u4e2a\u4eba\u4fe1\u606f\u4fee\u6539");
        label1.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 18));
        add(label1);
        label1.setBounds(new Rectangle(new Point(285, 35), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u59d3\u540d\uff1a");
        add(label2);
        label2.setBounds(new Rectangle(new Point(195, 135), label2.getPreferredSize()));
        add(textField1);
        textField1.setBounds(305, 130, 200, textField1.getPreferredSize().height);

        //---- label3 ----
        label3.setText("\u6027\u522b\uff1a");
        add(label3);
        label3.setBounds(195, 175, 39, 17);

        //---- label4 ----
        label4.setText("\u8054\u7cfb\u65b9\u5f0f\uff1a");
        add(label4);
        label4.setBounds(195, 215, 85, 17);
        add(textField3);
        textField3.setBounds(305, 210, 200, textField3.getPreferredSize().height);
        add(comboBox1);
        comboBox1.setBounds(305, 170, 200, comboBox1.getPreferredSize().height);

        //---- button1 ----
        button1.setText("\u4fee\u6539");
        button1.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 20));
        add(button1);
        button1.setBounds(250, 280, 178, 70);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    @Override
    public void initData() {
        Admin adminByUsername = UserDao.findAdminById(MainFrameConfig.getId());
        textField1.setText(adminByUsername.getA_name());
        if ("男".equals(adminByUsername.getSex())) {
            comboBox1.setSelectedIndex(0);
        } else {
            comboBox1.setSelectedIndex(1);
        }
        textField3.setText(adminByUsername.getContact());
    }

    @Override
    public void initListen() {
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Admin admin = new Admin();
                admin.setA_id(MainFrameConfig.getId());
                admin.setA_name(textField1.getText());
                admin.setSex(sex[comboBox1.getSelectedIndex()]);
                admin.setContact(textField3.getText());
                if (UserDao.updateAdmin(admin) > 0) {

                    showToast("修改成功");
                    initData();
                } else {
                    showToast("修改失败");
                }

            }
        });
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JLabel label3;
    private JLabel label4;
    private JTextField textField3;
    private JComboBox comboBox1;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
